import { Injectable } from '@nestjs/common'
import { ConfigService } from '@nestjs/config'
import { PassportStrategy } from '@nestjs/passport'
import { ExtractJwt, Strategy } from 'passport-jwt'
import { jwtKey } from 'src/config'

/*
 * @Author: cl
 * @Description: token 策略
 * @Date: 2023-05-04 11:45:26
 * @LastEditTime: 2023-05-04 17:19:00
 * @FilePath: \jsnode\nestjs\naota\src\user\strategy\jwt.strategy.ts
 * @LastEditors: cl
 */
@Injectable()
export class JwtToken extends PassportStrategy(Strategy, 'jwt') {
  constructor(){
    super({
      //解析用户提交的header中的Bearer Token数据
      jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
      //加密码的 secret
      secretOrKey: jwtKey.secret,
    })
    
  }
  async validate(payload: any) {
    console.log('校验通过',payload);
    return { userId: payload.sub, username: payload.username };
  }
}
